<%@page import="content.UserContent"%>
<%@page import="transfer.User"%>
<%@page import="java.util.Iterator"%>
<%@page import="transfer.UserRole"%>
<%@page import="java.util.ArrayList"%>
<%@page import="content.GroupContent"%>
<%@page import="transfer.Group"%>
<%
    String idGroupString = (String) request.getParameter("id");
    Integer idGroup = null;
    Group group = null;
    ArrayList users = null;
    if (idGroupString != null) {
        idGroup = Integer.valueOf(idGroupString);
        group = GroupContent.getGroup(idGroup);
        if (group == null) {
            %> 
                    <script>window.location.replace('admin.jsp?content=group');</script>
            <%
            return;
        }
        users = group.getUsers();
    }
    ArrayList roles = UserRole.getAllRoles();
%>

<div class="box"  id="group_id" <% if (idGroup != null) { %>  value="<%= idGroup %>" <% }%> >
    <table class="table-admin" rules=all>
        <tr>
            <%
                if (idGroupString == null) {
            %>
            <td colspan="2" style="text-align: left;">Create group</td>
            <%
                } else {
            %>
            <td colspan="2" style="text-align: left;">Change group</td>
            <%
                }
            %>
        </tr>
        <tr>
            <td>Name <span class="text-red">*</span></td>           
            <td><input type="text" id="name_group" required="" placeholder="Enter 2-40 symbols" maxlength="40" <% if (idGroup != null) { %>value="<%=group.getNameGroup()%>" <%}%> /><span class="error" id="nameTip"></span></td>
        </tr>
        <tr>
            <td>Role</td>
            <td>
                <input type="checkbox" id="apply"> apply a role to groups <br/>
                <select id="select_group_role">
                    <%
                        for (Iterator iterator = roles.iterator(); iterator.hasNext();) {
                            UserRole role = (UserRole) iterator.next();
                            if (!role.equals(UserRole.ADMIN)) {
                            %>                             
                            <option id="<%= role.getIdRole() %>"><%= role.getNameRole() %></option>
                            <%
                            }
                        }
                    %>
                </select>
            </td>
        </tr>
        <tr>
            <td>Users</td>
            <td>
                Filter: 
                <select id="select_filter_role" onchange="filter_role()">                
                    <option id="<%= UserRole.ALL.getIdRole() %>"><%= UserRole.ALL.getNameRole() %></option>
                    <%
                        for (Iterator iterator = roles.iterator(); iterator.hasNext();) {
                            UserRole role = (UserRole) iterator.next();
                            %>                             
                            <option id="<%= role.getIdRole() %>"><%= role.getNameRole() %></option>
                            <%
                        }
                    %>
                </select>
                <br/>
                <div style="vertical-align: top;">
                    <select multiple size="10" class="user-list" id="non_group_users">
                        <%
                        ArrayList allUsers = UserContent.getAllUsersForRole(UserRole.ALL);
                        if (idGroup != null) {
                            for (Iterator iterator = allUsers.iterator(); iterator.hasNext();) {
                                User user = (User) iterator.next();
                                if (!users.contains(user)) {
                                %>                             
                                <option id="<%= user.getIdUser() %>"><%= user.getFirstName() + " " + user.getLastName() %></option>
                                <%
                                }
                            }
                        } else {
                            for (Iterator iterator = allUsers.iterator(); iterator.hasNext();) {
                                User user = (User) iterator.next();
                                %>                             
                                <option id="<%= user.getIdUser() %>"><%= user.getFirstName() + " " + user.getLastName() %></option>
                                <%
                            }
                        }
                        %>
                    </select>
                    <div class="big-arrow" id="left-big-arrow">
                        >
                    </div>
                    <div class="big-arrow" id="right-big-arrow">
                        <
                    </div>
                    <select multiple="10" class="user-list" id="group_users">
                        <%
                        if (idGroup != null) {
                            for (Iterator iterator = users.iterator(); iterator.hasNext();) {
                                User user = (User) iterator.next();
                                %>                             
                                <option id="<%= user.getIdUser() %>"><%= user.getFirstName() + " " + user.getLastName() %></option>
                                <%
                            }
                        }
                        %>
                    </select>
                </div>
            </td>
        </tr>
    </table>
</div>